<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class CommentModel extends Model
{
    protected $table = 'comments';

    //插入文章的评论
    public function insertComment($data) {
        $res = self::insert($data);
        return $res;
    }

    //获取评论
    public function getComments($id) {
        $res = self::where('art_id',$id)->orderBy('id' , 'desc')->get()->toArray();
        return $res;
    }

    //获取评论（联表查询）
    public function getAllComments() {
        $res = DB::table('article as a')
                ->join('comments as c' , 'c.art_id' , '=' , 'a.id')
                ->select('c.id' , 'c.username' , 'c.content' , 'c.created_at' , 'a.title')
                ->orderBy('c.id' , 'desc')
                ->paginate(5);
        return $res;
    }

    //删除评论
    public function delComment($id) {
        $res = self::destroy('id' , $id);
        return $res;
    }
}
